package com.esun.payment_api.controller;

import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.domain.SysDictContract;
import com.esun.payment_api.domain.SysDictData;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.security.AjaxResult;
import com.esun.payment_api.service.SysDictContractService;
import com.esun.payment_api.service.SysDictDataService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.esun.common.utils.JWTUtils.getLoginUser;

@RestController
@RequestMapping("/sysDictContract")
public class SysDictContractController {

    @Resource
    SysDictContractService sysDictContractService;

    @Resource
    SysDictDataService sysDictDataService;

    /**
     * 查询所有合同字典信息，条件查询
     */
    @GetMapping("/querySysDictContractAll")
    public CommonResult querySysDictContractAll(Short coId, String keyword, String word) {
        try {
            List<SysDictContract> list = sysDictContractService.selectByExample(coId, keyword, word);
            return CommonResult.success(list, "合同字典查询成功");
        } catch (Exception e) {
            return CommonResult.failed("查询失败");
        }
    }

    /**
     * 添加单个合同
     */
    @RequestMapping("/addSysDictContractOne")
    public CommonResult addSysDictContractOne(@RequestBody SysDictContract record) {
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(user.getId())) {
            return CommonResult.failed("登录人id为空");
        }
        if (record != null) {
            record.setCreateBy(user.getId());
            record.setCreatedByUser((short) 1);
            record.setStatus("1");
            record.setIsDeleted("0");
            //检查合同编号是否为空以及其唯一性
            if (BlankUtils.isBlank(record.getCode()) || BlankUtils.isBlank(record.getCusId())) {
                return CommonResult.failed("合同编号、签订单位不可为空");
            }
            if (sysDictContractService.getSysDictContractByCode(record.getCode()).size() != 0) {
                return CommonResult.failed("合同编号必须是唯一的");
            }
        }
        try {
            int count = sysDictContractService.insertSelective(record);
            return CommonResult.success(count);
        } catch (Exception e) {
            return CommonResult.failed("添加失败");
        }
    }

    /**
     * 根据id删除单个(软删除)
     */
    @RequestMapping("/deleteSysDictContractById")
    public AjaxResult deleteSysDictContractById(Integer id) {
        try {
            return sysDictContractService.deleteById(id);
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 根据id修改单个
     */
    @RequestMapping("/updateSysDictContract")
    public AjaxResult updateSysDictContract(@RequestBody SysDictContract record) {
        try {
            return sysDictContractService.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            return AjaxResult.error("修改失败");
        }
    }

    @ApiOperation("新建合同-币种类型下拉框，支持类型值模糊查询")
    @RequestMapping("/getCurrencyTypeList")
    public CommonResult getCurrencyTypeList(Short coId, String word) {
        try {
            List<SysDictData> list = sysDictDataService.selectByExample(coId, "CURRENCY_CODE", "dictValue", word);
            return CommonResult.success(list, "币种类型查询成功");
        } catch (Exception e) {
            return CommonResult.failed("查询失败");
        }
    }

    @ApiOperation("新建合同-合同类型下拉框，支持类型值模糊查询")
    @RequestMapping("/getContractTypeList")
    public CommonResult getContractTypeList(Short coId, String word) {
        try {
            List<SysDictData> list = sysDictDataService.selectByExample(coId, "ht_type", "dictValue", word);
            return CommonResult.success(list, "合同类型查询成功");
        } catch (Exception e) {
            return CommonResult.failed("查询失败");
        }
    }

    @ApiOperation("新建合同-合同状态下拉框，支持类型值模糊查询")
    @RequestMapping("/getContractStatusList")
    public CommonResult getContractStatusList(Short coId, String word) {
        try {
            List<SysDictData> list = sysDictDataService.selectByExample(coId, "ht_status", "dictValue", word);
            return CommonResult.success(list, "合同状态类型查询成功");
        } catch (Exception e) {
            return CommonResult.failed("查询失败");
        }
    }

}
